//
//  OrderProductEntity.m
//  SXH
//
//  Created by Jack on 15/9/8.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "OrderProductEntity.h"

@implementation OrderProductEntity

@synthesize product_class,product_id,product_name,price,album_thumb,introduce,count;

- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.product_class= ([attributes valueForKeyPath:@"product_class"]==[NSNull null])?@"NULL":[attributes valueForKeyPath:@"product_class"];
    self.product_id = ([attributes valueForKeyPath:@"product_id"]==[NSNull null])?@"NULL":[attributes valueForKeyPath:@"product_id"];
    self.product_name = ([attributes valueForKeyPath:@"product_name"]==[NSNull null])?@"NULL":[attributes valueForKeyPath:@"product_name"];
    self.price = ([attributes valueForKeyPath:@"price"]==[NSNull null])?@"NULL":[attributes valueForKeyPath:@"price"];
    self.introduce = ([attributes valueForKeyPath:@"introduce"]==[NSNull null])?@"NULL":[attributes valueForKeyPath:@"introduce"];
    self.count = ([attributes valueForKeyPath:@"count"]==[NSNull null])?@"NULL":[attributes valueForKeyPath:@"count"];
    self.tourism_data = ([attributes valueForKeyPath:@"tourism_data"]==[NSNull null])?@"NULL":[attributes valueForKeyPath:@"tourism_data"];
    NSRange range = [[attributes valueForKeyPath:@"album_thumb"] rangeOfString:@"http://"];
    if(range.length==0){
        self.album_thumb = [NSString stringWithFormat:@"%@/%@", IMAGE_URL, [attributes valueForKeyPath:@"album_thumb"]];
    }else{
        self.album_thumb = [attributes valueForKeyPath:@"album_thumb"];
    }
    
    return self;
}

/**
 *  将对象写入文件的时候调用
 *  怎样存储
 *
 *  @param aCoder aCoder
 */
- (void)encodeWithCoder:(NSCoder *)encoder{
    //    [encoder encodeObject:self forKey:@"orderProductEntity"]; //encode 对象只能是字符串
    [encoder encodeObject:self.product_class forKey:@"product_class"];
    [encoder encodeObject:self.product_id forKey:@"product_id"];
    [encoder encodeObject:self.product_name forKey:@"product_name"];
    [encoder encodeObject:self.price forKey:@"price"];
    [encoder encodeObject:self.introduce forKey:@"introduce"];
    [encoder encodeObject:self.count forKey:@"count"];
    [encoder encodeObject:self.tourism_data forKey:@"tourism_data"];

    NSRange range = [self.album_thumb rangeOfString:@"http://"];
    if(range.length==0){
        [encoder encodeObject:[NSString stringWithFormat:@"%@/%@", IMAGE_URL, self.album_thumb] forKey:@"album_thumb"];
    }else{
        [encoder encodeObject:self.album_thumb forKey:@"album_thumb"];
    }
}

- (id)initWithCoder:(NSCoder *)decoder{
    if(self = [super init]) {
        self.product_class = [decoder decodeObjectForKey:@"product_class"];
        self.product_id = [decoder decodeObjectForKey:@"product_id"];
        self.product_name = [decoder decodeObjectForKey:@"product_name"];
        self.price = [decoder decodeObjectForKey:@"price"];
        self.introduce = [decoder decodeObjectForKey:@"introduce"];
        self.count = [decoder decodeObjectForKey:@"count"];
        self.album_thumb = [decoder decodeObjectForKey:@"album_thumb"];
        self.tourism_data = [decoder decodeObjectForKey:@"tourism_data"];

    }
    
    return self;
}


@end
